<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="药品名称" prop="drugName"> 
          <el-input
            v-model="queryParams.drugName"
            placeholder="请输入药品名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="药品关键字"  prop="drugKeyword">
          <el-input
            v-model="queryParams.drugKeyword"
            placeholder="请输入药品关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="药品类型" prop="drugType">
          <el-select v-model="queryParams.drugType" placeholder="请选择药品类型" clearable style="width: 200px">
            <el-option
              v-for="dict in drug_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="处方类型" prop="drugPrescription">
          <el-select v-model="queryParams.drugPrescription" placeholder="请选择处方类型" clearable style="width: 200px">
            <el-option
              v-for="dict in drug_prescription"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂家" prop="factoryName">
          <el-select v-model="queryParams.factoryName" placeholder="请选择生产厂家" clearable style="width: 200px">
            <el-option
              v-for="dict in factoryList"
              :key="dict.value"
              :label="dict.factoryName"
              :value="dict.factoryId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
               <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 200px">
                  <el-option
                     v-for="dict in sys_normal_disable"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"
                  />
               </el-select>
        </el-form-item>
        <el-form-item label="库存状态" prop="drug_status">
               <el-select v-model="queryParams.drug_status" placeholder="库存状态" clearable style="width: 200px">
                  <el-option
                     v-for="dict in drug_status"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"
                  />
               </el-select>
            </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleOrderAdd"
            v-hasPermi="['medicine:drug:add']"
          >新增采购单</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleOrderCart"
            v-hasPermi="['medicine:drug:add']"
          >我的采购</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="drugList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="药品ID" align="center" prop="drugId" />
        <el-table-column label="药片名称" align="center" prop="drugName" />
        <el-table-column label="药品编码" align="center" prop="drugCode" />
        <el-table-column label="药片关键字" align="center" prop="drugKeyword" />
        <el-table-column label="生产厂家" align="center" prop="hisMedicineFactory.factoryName" />
        <el-table-column label="药品类型" align="center" prop="drugType">
          <template #default="scope">
            <dict-tag :options="drug_type" :value="scope.row.drugType"/>
          </template>
        </el-table-column>
        <el-table-column label="处方类型" align="center" prop="drugPrescription">
          <template #default="scope">
            <dict-tag :options="drug_prescription" :value="scope.row.drugPrescription"/>
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center" prop="drugUnit" />
        <el-table-column label="销售价格" align="center" prop="drugPrice" />
        <el-table-column label="库存数量" align="center" prop="drugNum">
          <template #default="scope">
            <span :style="scope.row.drugNum<=scope.row.drugWarn?'color:red;font-weight:bold':''">
              {{scope.row.drugNum}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="预警值" align="center" prop="drugWarn" />
        <el-table-column label="状态" align="center" prop="status">
               <template #default="scope">
                  <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
               </template>
            </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="库存状态" align="center" prop="drug_status">
               <template #default="scope">
                  <dict-tag :options="drug_status" :value="scope.row.drugStatus" />
               </template>
            </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleOrderAdd(scope.row)" v-hasPermi="['medicine:order:edit']">添加采购</el-button>

          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改药品信息对话框 -->
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="drugRef" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="药片名称" prop="drugName">
            <el-input v-model="form.drugName" placeholder="请输入药片名称" />
          </el-form-item>
          <el-form-item label="药品编码" prop="drugCode">
            <el-input v-model="form.drugCode" placeholder="请输入药品编码" />
          </el-form-item>
          <el-form-item label="药片关键字" prop="drugKeyword">
            <el-input v-model="form.drugKeyword" placeholder="请输入药片关键字" />
          </el-form-item>
          <el-form-item label="生产厂家ID" prop="factoryId" >
            <el-select v-model="form.factoryId" placeholder="请选择生产厂家" style="width: 200px">
              <el-option
                v-for="dict in factoryList"
                :key="dict.value"
                :label="dict.factoryName"
                :value="dict.factoryId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="药品类型" prop="drugType">
            <el-select v-model="form.drugType" placeholder="请选择药品类型" style="width: 200px">
              <el-option
                v-for="dict in drug_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="处方类型" prop="drugPrescription">
            <el-select v-model="form.drugPrescription" placeholder="请选择处方类型" style="width: 200px">
              <el-option
                v-for="dict in drug_prescription"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="单位" prop="drugUnit">
            <el-select v-model="form.drugUnit" placeholder="请选择单位" style="width: 200px">
              <el-option
                v-for="dict in drug_unit"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="销售价格" prop="drugPrice">
            <el-input v-model="form.drugPrice" placeholder="请输入销售价格--预留字段" />
          </el-form-item>
          <el-form-item label="库存数量" prop="drugNum">
            <el-input v-model="form.drugNum" placeholder="请输入库存数量" />
          </el-form-item>
          <el-form-item label="预警值" prop="drugWarn">
            <el-input v-model="form.drugWarn" placeholder="请输入预警值" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="Drug">
  import { listDrug} from "@/api/his/drug";
  import { insertCart} from "@/api/his/orderCart";
  import { listAllFactory } from "@/api/his/factory";
  const router=useRouter();
  const { proxy } = getCurrentInstance();
  const { drug_type, drug_prescription,drug_unit,drug_status } = proxy.useDict('drug_type', 'drug_prescription','drug_unit','drug_status');
  const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
  
  const drugList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const factoryList=ref([]);
  const title = ref("");
  
  listAllFactory().then(res=>{
    factoryList.value= res.rows
  })
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      drugName: null,
      drugKeyword: null,
      drugType: null,
      factoryId:null,
      hisMedicineFactory:null,
      drug_unit: null,
      drugPrescription: null,
      status: null,
      drug_status:null
    },
    rules: {
      drugCode: [
        { required: true, message: "药品编码不能为空", trigger: "blur" }
      ],
      drugKeyword: [
        { required: true, message: "药片关键字不能为空", trigger: "blur" }
      ],
      factoryId: [
        { required: true, message: "生产厂家不能为空", trigger: "blur" }
      ],
      drugType: [
        { required: true, message: "药品类型不能为空", trigger: "change" }
      ],
      drugPrescription: [
        { required: true, message: "处方类型不能为空", trigger: "change" }
      ],
      drugUnit: [
        { required: true, message: "单位不能为空", trigger: "blur" }
      ],
      drugNum: [
        { required: true, message: "库存数量不能为空", trigger: "blur" }
      ],
      drugWarn: [
        { required: true, message: "预警值不能为空", trigger: "blur" }
      ],
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询药品信息列表 */
  function getList() {
    loading.value = true;
    listDrug(queryParams.value).then(response => {
      drugList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  // 表单重置
  function reset() {
    form.value = {
      drugId: null,
      drugName: null,
      drugCode: null,
      drugKeyword: null,
      factoryId: null,
      drugType: null,
      drugPrescription: null,
      drugUnit: null,
      drugPrice: null,
      drugNum: null,
      drugWarn: null,
      status: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      remark: null
    };
    proxy.resetForm("drugRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.drugId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  //新增采购药品

  /** 新增按钮操作 */
  function handleOrderAdd(row) {
    const _drugIds = row.orderId || ids.value;
    insertCart(_drugIds).then(res => {
        if(res.code){
            proxy.$modal.msgSuccess("添加成功");
        }
  })
  }
  //查看我的采购
  function handleOrderCart(){
    router.push('/medicine/cart/cartInfo')
  }
 

  getList();
  </script>
  